package com.rams.jhms.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import com.rams.jhms.Config;
import com.rams.jhms.gui.components.FileSelector;
import com.rams.jhms.gui.datamodels.ArtistListModel;

import net.miginfocom.layout.AC;
import net.miginfocom.layout.CC;
import net.miginfocom.layout.LC;
import net.miginfocom.swing.MigLayout;

public class AddArtistForm extends JDialog {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -9058316897136884593L;
	
	private JLabel artName = new JLabel("Name:");
	private JLabel artFSPath = new JLabel("Path:");
	private ArtistListModel artListModel = new ArtistListModel();
	private JList artsList = new JList(artListModel);
	private JScrollPane artListSC = new JScrollPane(artsList);
	private JTextField txtArtName = new JTextField();
	private JTextField txtArtPath = new JTextField();
	private JButton btnGetPath = new JButton("...");
	private JButton btnAdd = new JButton("Add");
	private JButton btnClose = new JButton("Close");
	
	private AddArtistForm instance;
	
	public AddArtistForm(JDialog form, String artName) {
		super(form);
		init(artName);
	}

	public AddArtistForm(JDialog form) {
		super(form);
		init(null);
	}

	private void init(String artName) {
		if (artName != null) {
			txtArtName.setText(artName);
		}
		instance = this;
		initGUI();
	}
	
	private void initGUI() {
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setModal(true);
		
		MigLayout ml = new MigLayout(new LC().debug(Config.layoutDebug),
                new AC().grow().growPrio(100, 0).shrink(),
                new AC());
		
		this.setLayout(ml);
		
		btnClose.setActionCommand("close");
		btnAdd.setActionCommand("add");
		btnGetPath.setActionCommand("path");
		
		txtArtName.getDocument().addDocumentListener(new TxtFieldListener());
		
		this.add(artName, new CC().cell(0, 0));
		this.add(txtArtName, new CC().cell(1, 0).grow());
		this.add(artFSPath, new CC().cell(0, 1));
		this.add(txtArtPath, new CC().cell(1, 1).grow());
		this.add(btnGetPath, new CC().cell(1, 1));
		this.add(btnAdd, new CC().cell(0, 2).grow().spanX());
		this.add(artListSC, new CC().cell(0, 3).grow().spanX());
		
		btnClose.addActionListener(new getPathAction());
		btnAdd.addActionListener(new getPathAction());
		btnGetPath.addActionListener(new getPathAction());
		
		this.pack();
		
		this.setLocationRelativeTo(null);
	}
	
	private class getPathAction implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			String command = e.getActionCommand();
			if (command.equals("path")) {
				
				// TODO: show relevant path selector
				return;
			} else if(command.equals("add")) {
				
			} else if(command.equals("close")) {
				instance.dispose();
			}
		}
		
	}

	private class TxtFieldListener implements DocumentListener {
		public void insertUpdate(DocumentEvent e) {
			artListModel.setArtistFilter(txtArtName.getText());
		}

		public void removeUpdate(DocumentEvent e) {
			artListModel.setArtistFilter(txtArtName.getText());
		}

		public void changedUpdate(DocumentEvent e) {
			/* ignore */
		}
		
	}
}
